package template.code;

import template.TextFieldInputListener;

import javax.swing.*;
import java.awt.event.*;

public class MyDialog extends JDialog {
    private JPanel contentPane;
    private JButton buttonOK;
    private JButton buttonCancel;
    private JTextField textField1;
    private JTextField textField2;
    private JTextField textField3;
    private JTextField textField4;
    private JCheckBox isRecyclerFragmentCheckBox;
    private DialogCallBack mCallBack;

    public MyDialog(DialogCallBack dialogCallBack) {
        this.mCallBack = dialogCallBack;
        setTitle("Mvp Create Helper");
        setContentPane(contentPane);
        setSize(600, 300);
        setLocationRelativeTo(null);
        setModal(false);

        //设置
        textField1.addCaretListener(new TextFieldInputListener());

        getRootPane().setDefaultButton(buttonOK);

        buttonOK.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onOK();
            }
        });

        buttonCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        });

        // call onCancel() when cross is clicked
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });

        // call onCancel() on ESCAPE
        contentPane.registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    }

    private void onOK() {
        // add your code here
        if (null != mCallBack) {
            mCallBack.ok(textField1.getText().trim(), textField2.getText().trim(), textField3.getText().trim(),textField4.getText().trim(), isRecyclerFragmentCheckBox.isSelected());
        }
    }

    private void onCancel() {
        // add your code here if necessary
        dispose();
    }

    public interface DialogCallBack {
        void ok(String moduleName, String author, String describe,String title, boolean isRecyclerFragment);
    }


//    public static void main(String[] args) {
//        MyDialog dialog = null;
//        MyDialog finalDialog = dialog;
//        dialog = new MyDialog(new DialogCallBack() {
//            @Override
//            public void ok(String moduleName, String author, String describe, String title,boolean isRecyclerFragment) {
//                System.out.print(moduleName + " ");
//                System.out.print(author + " ");
//                System.out.print(describe + " ");
//                System.out.print(isRecyclerFragment);
//            }
//        });
//        dialog.pack();
//        dialog.setVisible(true);
//    }
}
